package com.foo.common.base.utils;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.fluent.Configurations;

import com.google.common.base.MoreObjects;

public enum GlobalConfig2 {

	instace;

	private final org.slf4j.Logger logger = org.slf4j.LoggerFactory
			.getLogger(GlobalConfig2.class);

	private XMLConfiguration configuration;

	@Override
	public String toString() {
		return MoreObjects.toStringHelper(GlobalConfig2.class)
				.add("domainNames", getDomainNames()).add("imgUrl", getImgUrl())
				.add("imgUrlPre", getImgUrlPre())
				.add("emailSendingAddr", getEmailSendingAddr())
				.add("emailServer", getEmailServer())
				.add("emailUser", getEmailUser())
				.add("isSendSms", getIsSendSms())
				.add("financeEmail", getFinanceEmail()).toString();
	}

	public final Configuration getGlobalProperties() {

		if (configuration != null) {
			return configuration;
		}
		final String globalConfigPropertiesFileName = "globalConfig.xml";
		logger.info("load config file of:{} now.",
				globalConfigPropertiesFileName);

		Configurations configs = new Configurations();
		try {
			configuration = configs.xml(globalConfigPropertiesFileName);
		} catch (Exception e) {
			logger.error("ConfigurationException:{}", e);
			configuration = null;
		}
		if (configuration == null) {
			throw new RuntimeException("Read configuration of:【"
					+ globalConfigPropertiesFileName + "】 error.");
		}
		return configuration;
	}

	public String getDomainNames() {
		return getGlobalProperties().getString("domainNames");
	}

	public String getImgUrl() {
		return getGlobalProperties().getString("imgUrl");
	}

	public String getImgUrlPre() {
		return "http://" + getDomainNames() + getImgUrl();
	}

	public String getEmailSendingAddr() {
		return getGlobalProperties().getString("emailSendingAddr");
	}

	public String getEmailServer() {
		return getGlobalProperties().getString("emailServer");
	}

	public String getEmailUser() {
		return getGlobalProperties().getString("emailUser");
	}

	public String getEmailPassword() {
		return getGlobalProperties().getString("emailPassword");
	}

	public String getIsSendSms() {
		return getGlobalProperties().getString("isSendSms");
	}

	public String getFinanceEmail() {
		return getGlobalProperties().getString("financeEmail");
	}

}
